package com.lantosec.hmrp.modules.warning.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.warning.entity.WarningRule;
import com.lantosec.hmrp.modules.warning.service.IWarningRuleService;
import com.lantosec.hmrp.modules.warning.vo.WarningRuleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 预警规则 控制器
 *
 * @author AutoCode
 * @since 2020-05-19
 */
@Api(value = "预警规则控制器", tags = "预警规则控制器")
@RestController
@RequestMapping("/warning/rule")
@Log4j2
public class WarningRuleController extends BaseController {
    @Autowired
    private IWarningRuleService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<WarningRule>> all(
            @ApiParam(value = "预警规则分类id") @RequestParam(name = "warningId", required = false) String warningId) {
        List<WarningRule> list = service.list(new QueryWrapper<WarningRule>()
                .eq(StringUtils.isNotEmpty(warningId), String.valueOf(WarningRule.ColName.type_id), warningId));
        return Result.ok(list);
    }

    @GetMapping("query/{id}")
    @ApiOperation(value = "预警规则明细查询")
    public Result<Map<String, Object>> query(@ApiParam(value = "ID", required = true) @PathVariable(name = "id") String id) {
        Map<String, Object> param = service.queryDetails(id);
        return Result.ok(param);
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<WarningRuleVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name = "id") String id) {
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ApiOperation(value = "新增预警规则")
    @ControllerLog(name = "新增预警规则", type = LogType.update)
    public Result<String> create(
            @ApiParam(value = "预警规则VO对象", required = true) @RequestBody @Validated Map<String, Object> map,
            HttpServletRequest request,
            BindingResult bindingResult) {
        validationHandler(bindingResult);
        String roleId = service.create(map, request);
        return Result.ok(roleId);
    }

    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    @ControllerLog(name = "修改预警规则", type = LogType.update)
    public Result<String> update(
            @ApiParam(value = "预警规则VO对象", required = true) @RequestBody Map<String, Object> map,
            HttpServletRequest request) {
        String roleId = service.updateInfo(map, request);
        return Result.ok(roleId);
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name = "ids") String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @PutMapping("changeState")
    @ApiOperation(value = "启用/禁用")
    @ControllerLog(name = "启用/禁用预警规则", type = LogType.update)
    public Result<Void> changeState(
            @ApiParam(value = "预警规则ID", required = true) @RequestBody String param) {
        JSONObject jsonObject = JSONObject.parseObject(param);
        String id = jsonObject.getString("id");
        Integer state = Integer.parseInt(jsonObject.getString("state"));
        service.changeState(id, state);
        return Result.ok();
    }
}
